package ${model.packageName}.${model.modulePackage}.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import com.okrsoa.commons.model.Member;
import com.okrsoa.commons.util.DateUtil;
import com.okrsoa.commons.util.ServletUtil;
import com.okrsoa.commons.base.BaseServiceImpl;
import com.okrsoa.commons.cache.MyCache;
import com.okrsoa.commons.config.MsgConfig;
import com.okrsoa.commons.model.DataGrid;
import com.okrsoa.commons.model.PageParam;
import ${model.packageName}.${model.modulePackage}.bean.${model.model};
import ${model.packageName}.${model.modulePackage}.service.I${model.model}Service;
import com.sun.org.apache.commons.beanutils.BeanUtils;

/**
* ${model.model}ServiceImpl: ${model.comment} 业务服务层实现
* @author ${model.author}  ${date}
*/
@Service
public class ${model.model}ServiceImpl extends BaseServiceImpl implements I${model.model}Service {
	/**
	 * 查询
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public DataGrid selectQuery(Map<String, Object> params ,${model.model} ${model.modelL},PageParam param){
		DataGrid data=new DataGrid();
		StringBuffer sb=new StringBuffer("from ${model.model} model where 1=1");
		List list=new ArrayList();
				 	
#foreach($field in $fieldList)
#if($field.matchType == 'dateRange'||$field.matchType == 'numberRange')
		String ${field.field}Start = (String) params.get("${field.field}Start");
	   if(!StringUtils.isEmpty(${field.field}Start)){
	       sb.append(" and ${field.columnName} >= ?");
	       list.add(${field.field}Start);
	   }
	   String ${field.field}End = (String) params.get("${field.field}End");
	   if(!StringUtils.isEmpty(${field.field}End)){
	       sb.append(" and ${field.columnName} < ? ");
	       list.add(${field.field}End);
	   }
#elseif($field.type=="String"&&${field.field}!="id"&&${field.field}!="createUser"&&${field.field}!="createTime"
&&${field.field}!="updateUser"&&${field.field}!="updateTime")
#if($field.matchType == 'any')
	    if(StringUtils.isNotBlank(${model.modelL}.get${field.fieldUp}())){
	        sb.append(" and model.${field.field} like ?");
	        list.add("%"+${model.modelL}.get${field.fieldUp}()+"%");
	    }
#elseif($field.matchType == 'eq')
	    if(StringUtils.isEmpty(${model.modelL}.get${field.fieldUp}())){
	        sb.append(" and model.${field.field} = ? ");
	        list.add(${model.modelL}.get${field.fieldUp}());
	    }
#elseif($field.matchType == 'left')
	    if(StringUtils.isEmpty(${model.modelL}.get${field.fieldUp}())){
	        sb.append(" and model.${field.field} like ? ");
	        list.add(${model.modelL}.get${field.fieldUp}()+"%");
	    }
#elseif($field.matchType == 'right')
	    if(StringUtils.isEmpty(${model.modelL}.get${field.fieldUp}())){
	        sb.append(" and model.${field.field} like ? ");
	        list.add("%"+${model.modelL}.get${field.fieldUp}());
	    }
#else
		 if(StringUtils.isNotBlank(${model.modelL}.get${field.fieldUp}())){
			sb.append(" and model.${field.field} = ? ");
			list.add(${model.modelL}.get${field.fieldUp}());
		}
#end
#end
#end
		data.setTotal((Long)dao.findUniqueOne("select count(*)"+sb,list));
		param.appendOrderBy(sb);//排序.
		List<${model.model}> listResult=dao.findPage(sb.toString(), param.getPage(), param.getRows(), list);
		//for(int i=0;i<listResult();i++){
		//	${model.model} ${model.modelL}=listResult.get(i);
		//	${model.modelL}.setType(MyCache.getInstance().getSelectValue(${model.modelL}.getType())); 
		//}
		data.setRows(listResult);
		return data;
	}
	/**
	 * 查询总数
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Long selectCount(Map<String, Object> params,${model.model} ${model.modelL}){
		StringBuffer sb=new StringBuffer("select  count(*)  from ${model.model} where 1=1");
		List list=new ArrayList();
#foreach($field in $fieldList)
#if($field.matchType == 'dateRange'||$field.matchType == 'numberRange')
		String ${field.field}Start = (String) params.get("${field.field}Start");
	   if(!StringUtils.isEmpty(${field.field}Start)){
	       sb.append(" and ${field.columnName} >= ?");
	       list.add(${field.field}Start);
	   }
	   String ${field.field}End = (String) params.get("${field.field}End");
	   if(!StringUtils.isEmpty(${field.field}End)){
	       sb.append(" and ${field.columnName} < ? ");
	       list.add(${field.field}End);
	   }
#elseif($field.type=="String"&&${field.field}!="id"&&${field.field}!="createUser"&&${field.field}!="createTime"
&&${field.field}!="updateUser"&&${field.field}!="updateTime")
#if($field.matchType == 'any')
	    if(StringUtils.isNotBlank(${model.modelL}.get${field.fieldUp}())){
	        sb.append(" and model.${field.field} like ?");
	        list.add("%"+${model.modelL}.get${field.fieldUp}()+"%");
	    }
#elseif($field.matchType == 'eq')
	    if(StringUtils.isEmpty(${model.modelL}.get${field.fieldUp}())){
	        sb.append(" and model.${field.field} = ? ");
	        list.add(${model.modelL}.get${field.fieldUp}());
	    }
#elseif($field.matchType == 'left')
	    if(StringUtils.isEmpty(${model.modelL}.get${field.fieldUp}())){
	        sb.append(" and model.${field.field} like ? ");
	        list.add(${model.modelL}.get${field.fieldUp}()+"%");
	    }
#elseif($field.matchType == 'right')
	    if(StringUtils.isEmpty(${model.modelL}.get${field.fieldUp}())){
	        sb.append(" and model.${field.field} like ? ");
	        list.add("%"+${model.modelL}.get${field.fieldUp}());
	    }
#else
		 if(StringUtils.isNotBlank(${model.modelL}.get${field.fieldUp}())){
			sb.append(" and model.${field.field} = ? ");
			list.add(${model.modelL}.get${field.fieldUp}());
		}
#end
#end
#end
		return (Long)dao.findOne(sb.toString(), list);
	}

  	/**
	 * 保存
	 */
	@Override
	public String save(${model.model} ${model.modelL}) {
		Member me=ServletUtil.getMember();
		${model.modelL}.setCreateTime(DateUtil.currentTimestamp());
		${model.modelL}.setCreateUser(me.getId());
		
		if(null!=${model.modelL}.getId()){
		
		} 
		
		if(dao.save(${model.modelL})){
			saveLog("添加${model.comment}", " 编码："+${model.modelL}.getId());
			return  MsgConfig.MSG_KEY_SUCCESS;
		}else{
			return MsgConfig.MSG_KEY_FAIL;
		}
	}
	/** 
	 * 保存
	 */
	@Override
	public String saveOrUpdate(${model.model} ${model.modelL}) {
		Member me=ServletUtil.getMember();
		
		if(null!=${model.modelL}.getId()){ 
			${model.model} ${model.modelL}_old=dao.get(${model.model}.class, ${model.modelL}.getId());
			if(${model.modelL}_old!=null){
#foreach($field in $fieldList)
#if(${field.field}=="updateUser")
				${model.modelL}_old.setUpdateUser(me.getId());
#elseif(${field.field}=="updateTime")
				${model.modelL}_old.setUpdateTime(DateUtil.currentTimestamp());
#elseif(${field.isPk}!='Y'&&${field.field}!="code"&&${field.field}!="updateCode"&&${field.field}!="updateTime"&&${field.inputType}!="none")
				${model.modelL}_old.set${field.fieldUp}(${model.modelL}.get${field.fieldUp}());
#end
#end
				if(dao.update(${model.modelL}_old)){
					saveLog("修改${model.comment}", " 编码："+${model.modelL}.getId());
					return  MsgConfig.MSG_KEY_SUCCESS;
				}else{
					return MsgConfig.MSG_KEY_FAIL;
				}
			}else{
				${model.modelL}.setCreateTime(DateUtil.currentTimestamp());
				${model.modelL}.setCreateUser(me.getId());	
				${model.modelL}.setUpdateTime(DateUtil.currentTimestamp());
				${model.modelL}.setUpdateUser(me.getId()); 
				if(dao.save(${model.modelL})){
					saveLog("添加${model.comment}", " 编码："+${model.modelL}.getId());
					return  MsgConfig.MSG_KEY_SUCCESS;
				}else{
					return MsgConfig.MSG_KEY_FAIL;
				}
				
			}
			
		}
		return null;
	}

	/** 
	 * 修改
	 */
	@Override
	public String update(${model.model} ${model.modelL}) {
	 
		Member me=ServletUtil.getMember(); 
		${model.modelL}.setUpdateTime(DateUtil.currentTimestamp());
		${model.modelL}.setUpdateUser(me.getId());
		if(dao.update(${model.modelL})){
			saveLog("修改${model.comment}", " 编码："+${model.modelL}.getId());
			return  MsgConfig.MSG_KEY_SUCCESS;
		}else{
			return MsgConfig.MSG_KEY_FAIL;
		}
	}
	/**
	 * 批量删除
	 */
	public boolean delete(String[] ids){
		List<Object> list=new ArrayList<Object>();
		for(String id:ids){
			${model.model} ${model.modelL}=dao.get(${model.model}.class, id);
			list.add(${model.modelL});
		}
		return dao.deleteAll(list);
	}
}

